package com.topcent.tioxyc.xxljob;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.topcent.tioxyc.constants.KeyConstant;
import com.topcent.tioxyc.pojo.StaffEntryTime;
import com.topcent.tioxyc.util.ApiClient;
import com.topcent.tioxyc.util.ConnmeUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @author: 陈百万
 * @date: 2025/3/24 10:23
 * @Description: 每日计算员工入职周年时间
 */
@Slf4j
@Component
public class StaffEntryTimeXxlJob {

    @XxlJob("staffEntryTime")
    public String staffEntryTime() {
        List<StaffEntryTime> staffEntryTimeList = new ArrayList<>();
        Header[] headers = ConnmeUtils.createHeaders();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", "67d92f30a40cbadf87555cc4");
        // 2025年人员名单：67d92f9aa3fa7a68eb12daf1
        parameter.put("entry_id", "67d92f9aa3fa7a68eb12daf1");

        boolean fale = true;
        String zh = "";
        do {
            if (!zh.isEmpty()) {
                parameter.put("data_id", zh);
            }
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<StaffEntryTime> list = JSONUtil.toList(objects, StaffEntryTime.class);
            if (list.size() == 100) {
                zh = list.getLast().get_id();
            } else {
                fale = false;
            }
            staffEntryTimeList.addAll(list);
        } while (fale);

        for (StaffEntryTime staffEntryTime : staffEntryTimeList) {
            Date entryDateTime = staffEntryTime.get_widget_1742286746198();
            if (entryDateTime == null) {
                log.warn("员工 {} 的入职年份为空，跳过", staffEntryTime.get_widget_1742286746195());
                continue;
            }
            // 把 String 转换成 LocalDate
            LocalDate entryDate = entryDateTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate today = LocalDate.now();

            // 处理闰年 2 月 29 日的特殊情况
            if (entryDate.getMonthValue() == 2 && entryDate.getDayOfMonth() == 29 && !today.isLeapYear()) {
                entryDate = entryDate.withDayOfMonth(28);
            }

            // 判断是否是入职周年
            if (entryDate.getMonthValue() == today.getMonthValue() && entryDate.getDayOfMonth() == today.getDayOfMonth()) {
                int anniversaryYear = (int) ChronoUnit.YEARS.between(entryDate, today);
                if(anniversaryYear == 0){continue;}
                log.info("🎉 员工 {} 今天是入职 {} 周年，更新入职年份字段", staffEntryTime.get_widget_1742286746195(), anniversaryYear);
                // 只有在周年时才更新入职年份
                staffEntryTime.set_widget_1742459174314(anniversaryYear);

                Map<String, Object> parameterInster = new HashMap<>();
                parameterInster.put("app_id", "67d92f30a40cbadf87555cc4");
                parameterInster.put("entry_id", "67d92f9aa3fa7a68eb12daf1");
                parameterInster.put("is_start_trigger", "true");
                parameterInster.put("data_id", staffEntryTime.get_id());
                Map<String, Object> request = new HashMap<>();
                Map<String, Object> value = new HashMap<>();
                value.put("value", anniversaryYear);
                request.put("_widget_1742459174314", value);
                parameterInster.put("data", request);
                String jsonSalee = JSONUtil.toJsonStr(parameterInster);
                //log.info("Modified sales order prepayment: {}", jsonSalee);
                ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalee);

            }
        }

        return "success====》》》》" + staffEntryTimeList.size();
    }

}
